import { ref } from 'vue'
import app from '@/app'
import { get } from '@/api/store/order'

import base from '@/app/model'

// 继承总模型
const model = new base;

// 分页数量
model.getData().page_size = 20;

// 查询发票类型
model.selectTicket = ticket => {
  model.getData().ticket = ticket
  model.page(0)
}

// 查询类型
model.where = ref({ key: 'code', value: '' })

// 生成查询数据
model.getWhere = () => {
  // 查询方式
  const where = { [model.where.value.key]: model.where.value.value }
  // 合并数据
  return Object.assign(where, model.getData())
}

model.get = () => {
  return new Promise((resolve, jeject) => {
    app.ui.loading()
    // 查询
    get(model.getWhere()).then(res => {
      model.setList(res.data)
      // 分页
      model.setData(Object.assign(model.getData(), {
        page: res.current_page,
        page_size: res.per_page,
        page_total: res.total
      }))
      resolve(res)
    }).catch(res => {
      jeject(res)
    }).finally(() => {
      app.ui.loadingClose()
    })
  })
}

model.page = (page) => {
  model.getData().page = page
  model.get()
}

// 导出发票查询
model.excel = page => {
  return new Promise((resolve, jeject) => {
    const data = model.getWhere()
    // 分页重置
    data.page = page
    // 查询
    get(data).then(res => {
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

// 查询发票 
model.selectTicket('')

export default model